<template>
  <div class="lee-table">
    <div class="header">
      <slot name="header">
        <div class="title">{{ title }}</div>
        <div class="handler">
          <slot name="headerHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      :data="listData"
      border
      style="width: 100%"
      v-bind="childrenProps"
      @selection-change="handleSelectionChange"
    >
      <!-- 选择按钮 -->
      <el-table-column
        v-if="showIndexColumn"
        type="selection"
        align="center"
        width="60"
      ></el-table-column>
      <!-- 序号 -->
      <el-table-column
        v-if="showSelectColumn"
        label="序号"
        type="index"
        align="center"
        width="60"
      ></el-table-column>
      <template v-for="propItem in propList" :key="propItem.prop">
        <el-table-column v-bind="propItem" align="center" show-overflow-tooltip>
          <!-- 作用域插槽 -->
          <template #default="scope">
            <!-- 动态绑定插槽名字 -->
            <slot :name="propItem.slotName" :row="scope.row">
              {{ scope.row[`${propItem.prop}`] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div v-if="showFooter" class="footer">
      <slot name="footer">
        <el-pagination
          :current-page="page.currentPage"
          :page-size="page.pageSize"
          :page-sizes="[10, 20, 30]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { PropType } from 'vue'
const props = defineProps({
  // table的数据
  listData: {
    type: Array,
    required: true
  },
  // 数据总数统计
  listCount: {
    type: Number,
    default: 0
  },
  // 分别给contentTableConfig里面的每一个属性定义类型
  title: {
    type: String,
    default: ''
  },
  // 是否显示选择框列
  showSelectColumn: {
    type: Boolean,
    default: false
  },
  // 是否显示序号列
  showIndexColumn: {
    type: Boolean,
    default: false
  },
  // 是否展示底部的分页器
  showFooter: {
    type: Boolean,
    default: true
  },
  propList: {
    // 转换一下，不然报unknown错误
    type: Array as PropType<any[]>,
    required: true
  },
  // 分页 来自双向绑定
  page: {
    type: Object,
    default: () => ({ currentPage: 0, pageSize: 10 })
  },
  // 表格展开行
  childrenProps: {
    type: Object,
    default: () => ({})
  }
})

const emits = defineEmits(['selectionChange', 'update:page']) // 触发自定义事件

// table多选按钮的函数 一般为外部调用，所以使用自定义事件来做
const handleSelectionChange = (value: any) => {
  let selectionArr: any[] = []
  // 拿到id数组用map加工 不要直接push
  selectionArr = value.map((item: any) => {
    return item.id
  })
  emits('selectionChange', selectionArr)
}
// 分页器的切换事件
const handleCurrentChange = (currentPage: number) => {
  emits('update:page', { ...props.page, currentPage })
}
const handleSizeChange = (pageSize: number) => {
  emits('update:page', { ...props.page, pageSize })
}
</script>

<style scoped lang="less">
.header {
  display: flex;
  height: 45px;
  padding: 0 5px;
  justify-content: space-between;
  align-items: center;

  .title {
    font-size: 20px;
    font-weight: 700;
  }

  .handler {
    align-items: center;
  }
}

.footer {
  margin-top: 15px;
  display: flex;
  justify-content: right;
}
</style>
